package com.wanxi.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

/**
 * 后台管理员
 * @author Chengbo Zhang
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Manager implements UserDetails{
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;
    private String managerName;
    private String password;
    private Integer enable;
    private Date createTime;
    private Date updateTime;
    private String remark;

    @TableField(exist = false)
    private Set<Role> roles=new HashSet<>();

    @TableField(exist = false)
    private Set<Permission> authorities =  new HashSet<>();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        for (Role role : roles) {
            if(Objects.nonNull(role.getPermissions()))
                authorities.addAll(role.getPermissions());
        }
        return authorities;
    }

    @Override
    public String getUsername() {
        return this.getManagerName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.getEnable() == 1;
    }
}
